package com.prj.ufdm.demo.tutorial.fabric;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.hyperledger.fabric.sdk.Channel;
import org.hyperledger.fabric.sdk.Enrollment;
import org.hyperledger.fabric.sdk.Orderer;
import org.hyperledger.fabric.sdk.Peer;
import org.hyperledger.fabric.sdk.TransactionRequest;

import com.prj.ufdm.demo.constant.DemoConstants;

public class TestFabricClient {

	FabricClient fabricClient;
	
	Peer peer;
	List<Peer> peers = new ArrayList<>();
	
	static String channelName = "mychannel";
	static String chainCodeName = "MyFirstChainCode";
	static String chainCodeVersion = "1.0";
	
	static Orderer tlscaOrder;

	// 查询合约
	public static void main(String[] args) throws Exception {

		TestFabricClient testFabric = new TestFabricClient();
		String invodeArgs[] = { "" };
		Map queryResult = null;
		
		// 初始化合约
		testFabric.initChaincode(TransactionRequest.Type.JAVA,channelName,chainCodeName,chainCodeVersion,"initUsers", invodeArgs);
		 
		// 查询所有 
//		queryResult = testFabric.queryChaincode(channelName,TransactionRequest.Type.JAVA, chainCodeName,"queryAllUsers",invodeArgs); 
//		System.out.println("\n 查询所有 queryResult:"+queryResult);
		 
		// 修改 invodeArgs = new String [] {"asset1", "bruce" };
//		testFabric.invokeChaincode(channelName,TransactionRequest.Type.GO_LANG, "basic","TransferAsset",invodeArgs);
		  
		// 查询单条 invodeArgs = new String [] {"asset1"}; queryResult =
//		testFabric.queryChaincode(channelName,TransactionRequest.Type.GO_LANG,"basic","ReadAsset",invodeArgs); 
//		System.out.println("\n 查询单条 queryResult:"+queryResult);
		
		// 安装合约
//		testFabric.installChaincode(TransactionRequest.Type.JAVA,chainCodeName, "1.0", "E:\\project_new\\fabric\\my-test-chain-code");
		

		
	}

	public Channel createChannel(String channelName, String txPath) throws Exception {
		return fabricClient.createChannel(channelName, tlscaOrder, txPath);
	}
	
	public Map queryChaincode(String channelName, TransactionRequest.Type lang, String chaincodeName, String funcName,String invodeArgs[]) throws Exception {
		return fabricClient.queryChaincode(peers, channelName, lang, chaincodeName, funcName,invodeArgs);
	}

	public void invokeChaincode(String channelName, TransactionRequest.Type lang, String chaincodeName, String funcName,String invodeArgs[]) throws Exception {
		fabricClient.invoke(channelName, lang, chaincodeName, tlscaOrder, peers, funcName,invodeArgs);
	} 

	public void installChaincode(TransactionRequest.Type lang,String chaincodeName, String chaincodeVersion, String chaincodeLocation) throws Exception {
		
		System.out.println("\n 开始安装..............start");
		fabricClient.installChaincode(lang, chaincodeName, chaincodeVersion, chaincodeLocation,peers);
		System.out.println("\n 安装完成---------end");
	}
	
	public void initChaincode(TransactionRequest.Type lang,String channelName,String chainCodeName,String chainCodeVersion,String funcName, String invodeArgs[]) throws Exception {
		System.out.println("\n 开始实例化.............start");
		fabricClient.initChaincode(channelName,lang, chainCodeName, chainCodeVersion, tlscaOrder, peer, funcName, invodeArgs);
		System.out.println("\n 实例化完成---------end");
	}

	
	TestFabricClient() throws Exception {
		UserContext userDetail = new UserContext();
		userDetail.setAffiliation("Org1");
		userDetail.setMspId("Org1MSP");
		// userDetail.setAccount("huyz");
		userDetail.setName("admin");
		Enrollment enrollment = BuildEnrollment.getEnrollment();
		userDetail.setEnrollment(enrollment);
		fabricClient = new FabricClient(userDetail);
		peers = new ArrayList<>();
		Peer peer0 = fabricClient.getPeer("peer0.org1.example.com", "grpcs://peer0.org1.example.com:7051",DemoConstants.FABRIC_ORG1_PEER0_CRT);
		Peer peer1 = fabricClient.getPeer("peer0.org2.example.com", "grpcs://peer0.org2.example.com:9051",DemoConstants.FABRIC_ORG2_PEER0_CRT);
		peer = peer0;
		peers.add(peer0);
		peers.add(peer1);
		tlscaOrder = fabricClient.getOrderer("orderer.example.com", "grpcs://orderer.example.com:7050",DemoConstants.FABRIC_ORDER_TLS_PEM);
	}

}
